package dao;

import java.util.ArrayList;
import java.util.List;

import entity.Notes;
import entity.OnteComment;

public class NotesDao extends BaseDao {

	/**
	 *  所有笔记
	 * @return
	 */
	public List<Notes> selectNotes(){
		try {
			rs=executeQuery("select notes.*,`user`.userName,`user`.avatar from notes,`user` where notes.userId = `user`.userId");
			List<Notes> notesList=new ArrayList<Notes>();
			while(rs.next()) {
				Notes notes=new Notes();
				notes.setNotesId(rs.getInt("notesId"));
				notes.setTitle(rs.getString("title"));
				notes.setContent(rs.getString("content"));
				notes.setPicture(rs.getString("picture"));
				notes.setUsername(rs.getString("username"));
				notes.setTime(rs.getDate("time"));
				notes.setLike(rs.getInt("like"));
				notes.setCollection(rs.getInt("collection"));
				notes.setCountComment(rs.getInt("countComment"));
				notes.setAvatar(rs.getString("avatar"));
				notesList.add(notes);
			}
			return notesList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据id查笔记
	 * @param notesId
	 * @return
	 */
	public Notes notesId(int notesId) {
		try {
			rs=executeQuery("SELECT `user`.avatar,notes.picture,notes.title,notes.content,notes.`like`,notes.time,notes.collection,notes.countComment,`user`.userName FROM notes,`user` where notesId=? and `user`.userId=notes.userId",notesId);
			if(rs.next()) {
				Notes notes=new Notes();
				notes.setNotesId(notesId);
				notes.setTitle(rs.getString("title"));
				notes.setContent(rs.getString("content"));
				notes.setPicture(rs.getString("picture"));
				notes.setUsername(rs.getString("username"));
				notes.setTime(rs.getDate("time"));
				notes.setLike(rs.getInt("like"));
				notes.setCollection(rs.getInt("collection"));
				notes.setCountComment(rs.getInt("countComment"));
				notes.setAvatar(rs.getString("avatar"));
				return notes;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	/**
	 * 根据笔记id查询评论
	 * @param notesId
	 * @return
	 */
	public List<OnteComment> selectOnte(int notesId){
		try {
			rs=executeQuery("select ontecomment.*,`user`.userName,`user`.avatar from ontecomment,`user` where ontecomment.userId=`user`.userId AND notesId=?", notesId);
			List<OnteComment> onteList=new ArrayList<>();
			while(rs.next()){
				OnteComment p = new OnteComment(rs.getInt("commentId"), rs.getInt("notesId"), rs.getInt("userId"), rs.getString("comment"), rs.getDate("commentTime"),rs.getString("avatar"),rs.getString("username"));
				onteList.add(p);
			}
			return onteList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
}
